package com.patchmanage.back.controller;


import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patchmanage.back.entity.Cve;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.ICveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-06-18
 */
@Api(value = " 漏洞controller", tags = {"漏洞 Api"})
@RestController
@RequestMapping("/back/cve")
public class CveController {

    @Autowired
    private ICveService cveService;

    /**
     * 分页获取漏洞列表接口
     *
     * @param dict
     * @return
     */
    @ApiOperation(value = "分页获取漏洞列表接口", notes = "分页获取补丁列表接口")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cveId", value = "cveId", required = false, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "rhsaId", value = "rhsaId", required = false, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "pageNo", value = "pageNo", required = true, dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", required = true, dataType = "Integer", paramType = "body")
    })
    public Result<IPage<Cve>> list(@ApiIgnore @RequestBody Dict dict) {

        Page<Cve> page = new Page(dict.getInt("pageNo"), dict.getInt("pageSize"));
        QueryWrapper<Cve> queryWrapper = new QueryWrapper<>();
        if(dict.getStr("cveId") != null){
            queryWrapper.eq("cve_id", dict.getStr("cveId"));
        }
        if(dict.getStr("rhsaId") != null){
            queryWrapper.eq("rhsa_id", dict.getStr("rhsaId"));
        }
        queryWrapper.orderByDesc("publishing_time");
        return Result.success(cveService.page(page, queryWrapper));
    }

}
